package com.love.poetry.interfaces;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.love.poetry.application.CategoryService;
import com.love.poetry.domain.Category;
import com.youboy.core.orm.Page;
import com.youboy.core.web.controller.CrudControllerSupport;
import com.youboy.util.EntityUtils;


@Controller
@RequestMapping(value="/category")
public class CategoryController extends CrudControllerSupport<String, Category>{
	
	@Autowired
	private CategoryService categoryService;
	
	protected String getViewPackage() {
		return "/category";
	}
	private static final String REDIRECT_LIST = "redirect:/category/list/";
	
	@Override
	@RequestMapping(value="/create",method=RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute(new Category()).addAttribute("categoryNames", categoryService.queryCategories());
		return formView();
	}
	
	@Override
	public String create(Category arg0, BindingResult arg1) {
		return null;
	}
	
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public String create(Category entity, BindingResult result,HttpServletRequest request) {
		if(result.hasErrors()){
			System.out.println("category:create error");
			return null;
		}
		Category fatherCategory = categoryService.get(request.getParameter("belongCategory"));
        entity.setFather(fatherCategory);
		categoryService.save(entity);
		return REDIRECT_LIST;
	}

	@Override
	@RequestMapping(value="/{id}/delete/",method=RequestMethod.DELETE)
	public String delete(@PathVariable("id")String id) {
		try {
			categoryService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listView();
	}

	@Override
	@RequestMapping(value="/delete/",method=RequestMethod.DELETE)
	public String delete(HttpServletRequest request) {
        String[] items = EntityUtils.nullSafe(request.getParameterValues( "items"), new String[] {});
        
        for (String item : items) {
           delete(item);
       }
		return listView();
	}

	@Override
	@RequestMapping(value="/{id}/edit/",method=RequestMethod.GET)
	public String edit(@PathVariable("id")String id, Model model) {
		Category category=categoryService.get(id);
		model.addAttribute(category).addAttribute("categoryNames", categoryService.queryCategories()).addAttribute("_method", "PUT");
		return formView();
	}

	@Override
	@RequestMapping(value="/{id}/edit/",method=RequestMethod.PUT)
	public String edit(@PathVariable("id")String id, HttpServletRequest request) {
        try {
                   Category entity = categoryService.get(id);
                   entity.setFather(categoryService .get(request.getParameter( "belongCategory")));
                   bind(request, entity);
                   categoryService.update(entity);
                   success("分类修改成功" );
        } catch (Exception e) {
			e.printStackTrace();
			return null;
		}
        return REDIRECT_LIST ;
	}

	@RequestMapping(value="/list/",method=RequestMethod.GET)
	public String list(Page<Category> page,Model model){
		model.addAttribute(categoryService.queryPage(page));
		return listView();
	}
	
}